﻿(function() {
    var global_target = null;

    var tab_head_template = null;
    var tag_head_template = null;
    var tab_content_template = null;
    var tag_content_template = null;

    // 选项添加的时间轴
    var tabs = new Array();
    // 选项的序列
    var tabIndex = 0;
    // 标记是添加标签 不是 选择已有标签
    var fromadd = false;

    // tag显示内容div编号的前半部分
    var tag_content_preid = "workarea-";

    // 初始化参数
    function init_params() {
        tab_head_template = "<ul class='tab_tags'>[tabs]</ul>"
        tag_head_template = "<li id='[id]' class='tag_li'><a href='[href]' title='[title]'>[text]</a><a class='tab_close'>X</a></li>";
        tab_content_template = "<div class='tab_content'>[content]</div>";
        tag_content_template = "<div id='[id]' class='tag_content'>[text]</div>";
        tabs[tabIndex] = "1-1";
    }

    // 生成tab头部
    function produce_tab_head() {
        var lis = $(global_target).children("ul").children("li");
        var tag_head = "";
        $.each(lis, function(key, li) {
            var a = $(li).children("a");
            var title = $(a).attr("title");
            if (title == undefined)
                title = $(a).html();
            tag_head += tag_head_template.replace(/\[id\]/, $(li).attr("id")).replace(/\[title\]/, title).replace(/\[text\]/, $(a).html()).replace(/\[href\]/, $(a).attr("href"));
        })

        var tab_head = "";
        tab_head = tab_head_template.replace(/\[tabs\]/, tag_head);
        return tab_head;
    }

    // 生成tab体
    function produce_tab_content() {
        var divs = $(global_target).children(".tab_content").children("div");
        var tag_content = "";
        $.each(divs, function(key, div) {
            var value = $(div).html();
            value = value == null ? "" : value;
            tag_content += tag_content_template.replace(/\[id\]/, $(div).attr("id")).replace(/\[text\]/, value);
        })

        var tab_content = "";
        tab_content = tab_content_template.replace(/\[content\]/, tag_content);
        return tab_content;
    }

    // 生成tab
    function produce_tab() {
        init_params();
        var tab = "";
        tab += produce_tab_head();
        tab += produce_tab_content();
        return tab;
    }

    // 添加tab
    function add_tab(tabid, tabname) {
        global_target = $(this);
        var lis = $(global_target).children("ul").children("li");

        var addable = true;
        $.each(lis, function(key, li) {
            if ($(li).attr("id") == tabid)
                addable = false;
        });

        if (addable) {
            if (lis.length > 15) {
                alert("选项卡已满！");
                return false;
            }

            var tag_head = "";
            var shortname = tabname;
            var nameLength = tabname.length;
            if (nameLength >= 6)
                shortname = shortname.substring(0, 4) + "..";
            tag_head += tag_head_template.replace(/\[id\]/, tabid).replace(/\[title\]/, tabname).replace(/\[text\]/, shortname).replace(/\[href\]/, "#" + tag_content_preid + tabid);
            $(global_target).children("ul").append(tag_head);

            var tag_content = "";
            tag_content += tag_content_template.replace(/\[id\]/, tag_content_preid + tabid).replace(/\[text\]/, "");
            $(global_target).children(".tab_content").append(tag_content);
            fill_tab_list(tabid);


        }
        var tab = $("#" + tabid);
        tab_selected(tab);
        refresh_tab();
    }

    function fill_tab_list(tabid) {
        tabIndex++;
        tabs[tabIndex] = tabid;
        fromadd = true;
    }

    function set_tab_list_last_item(tabid) {
        if (!fromadd) {
            var count = tabs.length;
            for (var index = 0; index < count; index++) {
                if (tabs[index] == tabid && tabs[index + 1] != null) {
                    tabs[index] = tabs[(index + 1)];
                    tabs[index + 1] = tabid;
                }
            }
        }

        fromadd = false;
    }

    function get_last_select_item() {
        tabs[tabIndex] = null;
        tabIndex--;
        var ul = $(global_target).children("ul");
        var tab = $(ul).children("#" + tabs[tabIndex]);
        return tab;
    }

    function test() {
        var sada = "";
        for (var index = 0; index <= tabIndex; index++) {
            sada += tabs[index] + ",";
        }
        alert(sada);
    }

    // 删除tab
    function del_tag(tag) {
        var tagid = $(tag).parent().attr("id");
        global_target = $(tag).parent().parent().parent();
        var ul = $(global_target).children("ul");
        var liCount = $(ul).children("li").length;
        if (liCount > 1) {
            var id = $(ul).children("#" + tagid).children("a").attr("href");
            var cls = $(ul).children("#" + tagid).attr("class");
            tab_selected($(ul).children("#" + tagid));
            $(global_target).children(".tab_content").children("#" + id).remove();
            $(ul).children("#" + tagid).remove();
            var newtab = get_last_select_item();
            if ($(newtab).html() == null)
                newtab = $(ul).children("li:first");
            tab_selected(newtab);

        } else {
            alert("至少需要一个选项");  //当删除tab时，如果指定id没有也会显示这个
        }
    }

    // 选中tab修改class
    function tab_selected(tab) {
        var tabid = $(tab).attr("id");
        set_tab_list_last_item(tabid);
        global_target = $(tab).parent().parent();
        var lis = $(global_target).children("ul").children("li");
        $.each(lis, function(key, li) {
            $(li).removeClass("tag_selected");
        });
        $(global_target).children("ul").children("#" + tabid).addClass("tag_selected");

        var divs = $(global_target).children(".tab_content").children("div");
        $.each(divs, function(key, div) {
            $(div).removeClass("tag_selected");
        })

        var id = $(global_target).children("ul").children("#" + tabid).children("a").attr("href");
        $(global_target).children(".tab_content").children(id).addClass("tag_selected");

    }

    function hide_tag_close() {
        var lis = $(global_target).children("ul").children("li");
        $.each(lis, function(key, li) {
            $(li).children(".tab_close").remove();
        });
    }

    // 刷新tab
    function refresh_tab() {
        binding_event();
        var ul = $(global_target).children("ul");
        var lis = $(ul).children("li");
        var lilength = lis.length;
        var width = $(ul).width() / lilength;
        if (lilength > 8) {
            $.each(lis, function(index, li) {
                $(li).width(width);
            });
        }
    }


    $.fn.selftab = function(options) {
        global_target = $(this);
        $(global_target).html(produce_tab());

        var defalut_options = {
            tag_close: true
        };

        global_options = $.extend(defalut_options, options);

        if (!global_options.tag_close) {
            hide_tag_close();
        }

        var tab = $(global_target).children("ul").children("li:first");
        tab_selected(tab);

        refresh_tab();
    };

    // 绑定事件
    function binding_event() {
        var tags = $(global_target).children("ul").children("li");
        $(tags).unbind("click");
        $(tags).bind("click", function(event) { tab_selected($(this)); return false; });

        var spanClose = $(tags).children(".tab_close");
        $(spanClose).unbind("click");
        $(spanClose).bind("click", function() { del_tag($(this)); });
    }

    var global_options = null;

    $.fn.addtab = add_tab;

    $.fn.deltab = function(tag_id) {
        var target = $("#" + tag_id).children(".tab_close");
        del_tag(target);
    };

    $.fn.tagContentPreid = tag_content_preid;
})(jQuery);
